<?php
/**
 * Created by PhpStorm.
 * User: ulinix
 * Date: 2017/10/12
 * Time: 18:53
 */
namespace Home\Controller;
use Think\Controller;
class Home2018Controller extends Controller
{
    private static $url = ".shtml";

    public static function home_news($pid,$where='',$limit0='',$limit1='',$fields=''){
        //qong tur
        $map=array();
        $catids=self::getAllChildcateIds($pid);
        if($catids){
            $row=M("article")->where("status=1 and pid IN ($catids) " .$where)
                ->field($fields)
                ->order('intime desc')
                ->limit($limit0,$limit1)
                ->select();
        }else{
            $row=M("article")->where("status=1 and pid=".$pid .$where)
                ->field($fields)
                ->order('intime desc')
                ->limit($limit0,$limit1)
                ->select();
        }
        foreach ($row as $k=>$v){
            $v['url']="/view/".date("Y/m/",$v['intime']).$v['id'].self::$url;
            $map[]=$v;
        }
        return $map;
    }
    // //pid ha asasn hawar qikirix
   static function  getAllChildcateIds($categoryID)
    {
        //初始化ID数组
        $array[] = $categoryID;
        do{
            $ids = '';
            $cate = M('cate')->where("pid IN($categoryID)")->select();
            
            foreach ($cate as $k=>$v)
            {
                $array[] = $v['id'];
                $ids .= ',' . $v['id'];
            }
            $ids = substr($ids, 1, strlen($ids));
            $categoryID = $ids;

        }
        while (!empty($cate));
        $ids = implode(',', $array);
        return $ids;    //  返回字符串
        //return $array //返回数组
    }
    //获取指定分类所有父ID号
   static function getAllFcateIds($categoryID){
        //初始化ID数组
        $array[] = $categoryID;
        do{
            $ids = '';
            $where['id'] = array('in',$categoryID);
            $cate = M('cate')->where($where)->select();
//            echo M('cate')->_sql();
            foreach ($cate as $v){
                $array[] = $v['pid'];
                $ids .= ',' . $v['pid'];
            }
            $ids = substr($ids, 1, strlen($ids));
            $categoryID = $ids;
        }
        while (!empty($cate));
        $ids = implode(',', $array);
        return $ids;   //  返回字符串
        //return $array //返回数组
    }
    /**
     * turlarni ana bala munasiwitida tizidu
     * @param $items  所有的数据
     * @return array
     */
    function genTree5($items) {
        foreach ($items as $item)
            $items[$item['pid']]['son'][$item['id']] = &$items[$item['id']];
        return isset($items[0]['son']) ? $items[0]['son'] : array();
    }
    function genTree9($items) {
        $tree = array(); //格式化好的树
        foreach ($items as $item)
            if (isset($items[$item['pid']]))
                $items[$item['pid']]['list'][] = &$items[$item['id']];
            else
                $tree[] = &$items[$item['id']];
        return $tree;
    }
//删除runtime chache
    public static function remove_Cache(){
        //Runtime下删除admin里面的
        $cachedir = RUNTIME_PATH . "/Cache/Admin/";   //Cache文件的路径；
        if ($dh = opendir($cachedir)) {     //打开Cache文件夹；
            while (($file = readdir($dh)) !== false) {    //遍历Cache目录，
                unlink($cachedir . $file);    //删除遍历到的每一个文件；
            }
            closedir($dh);
        }
        //Runtime下删除Home里面的
        $cachedir = RUNTIME_PATH . "/Cache/Home/";   //Cache文件的路径；
        if ($dh = opendir($cachedir)) {     //打开Cache文件夹；
            while (($file = readdir($dh)) !== false) {    //遍历Cache目录，
                unlink($cachedir . $file);    //删除遍历到的每一个文件；
            }
            closedir($dh);
        }
        $tempdir = RUNTIME_PATH . "/Temp/";   //Cache文件的路径；
        if ($dh = opendir($tempdir)) {     //打开Cache文件夹；
            while (($file1 = readdir($dh)) !== false) {    //遍历Cache目录，
                unlink($tempdir . $file1);        //删除遍历到的每一个文件；
            }
            closedir($dh);
        }
    }

}